package com.mbatis.leeCode;

/**
 * @Classname LengthOfLastWord  最后一个单词的长度
 * @Date 2021/6/10 10:58
 * @Created by zhang.yuming
 */
public class LengthOfLastWord {


    public static void main(String[] args) {
        String str = "Hello World ";
        int lw = lofw(str);
        System.out.println("最后一个单词的长度="+lw);
    }

    public static int lofw(String str){
        int end = str.length() -1 ;
        if (end<=0){
            return 0;
        }
        while(end >= 0 && str.charAt(end) == ' '){
            end--;
        }
        int start = end;
        while(start>0 && str.charAt(start) != ' '){
            start--;
        }
        return end-start;
    }
}
